//
//  HelpClass.m
//  HondaPro_2
//
//  Created by Eric on 13-7-8.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "HelpClass.h"
#import "webConfig.h"
#import "webService.h"

@implementation HelpClass

+ (UIColor *)colorWithHexString: (NSString *)stringToConvert
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor clearColor];
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor clearColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

+(id)jsonParseByJsonFileStr:(NSString *)jsonFileStr
{
    NSLog(@"%@",jsonFileStr);
    NSData *data=[NSData dataWithContentsOfFile:jsonFileStr];
    NSError *error=nil;
    id jsonObject=[NSJSONSerialization JSONObjectWithData:data
                                                  options:NSJSONReadingAllowFragments
                                                    error:&error];
    if (jsonObject != nil && error == nil)
    {
        if ([jsonObject isKindOfClass:[NSDictionary class]])
        {
//            NSLog(@"NSDictionary jsonObject ->%@",(NSDictionary *)jsonObject);
            return jsonObject;
        }
        else if ([jsonObject isKindOfClass:[NSArray class]])
        {
//            NSLog(@"NSArray jsonObject ->%@",(NSArray *)jsonObject);
            return jsonObject;
        }else
        {
//            NSLog(@"jsonObject not NSDictionary||NSArray Class");
        }
    }
    return nil;
}

//根据json文件名和类型解析出数据，返回数据
+(id)returnTheDataByJsonFileAndJustForTestWithFileName:(NSString *)fileName andType:(NSString *)fileType
{
    NSString *jsonFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileType];
    
    return [HelpClass jsonParseByJsonFileStr:jsonFilePath];
}

//从Config.plist中读取数据
+(NSMutableDictionary *)getDicDataFromConfigFileFromDocumnetFolder
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *realPath = [documentPath stringByAppendingPathComponent:@"Config.plist"];
    return [[NSMutableDictionary alloc] initWithContentsOfFile:realPath];
}

/*webService请求数据*/
//请求网络数据
+(void)getWebServiceDataByString:(NSString *)strMsg andArrObject:(NSMutableArray *)arrWebData
{
    webService *web=[[webService alloc] init];
    web.methodName = strMsg;
    [web commonGetSharedRequest:strMsg wsparas:arrWebData];
}
/*end of webService请求数据*/

//设置状态button状态的方法,YES:选中，NO:未选中
+(void)setUpTheStateButtonState:(BOOL)state withBtn:(UIButton *)button
{
    if (YES == state)
    {
        //设置button的状态为选中
        [button setBackgroundImage:[UIImage imageNamed:@"mm_red_btn.png"] forState:UIControlStateNormal];
    }
    else
    {
        [button setBackgroundImage:[UIImage imageNamed:@"mm_gray_btn.png"] forState:UIControlStateNormal];
    }
}

+(void)setUpTheStateButtonEnableState:(BOOL)enable withBtn:(UIButton *)button
{
    [HelpClass setUpTheStateButtonState:enable withBtn:button];
    [button setEnabled:enable];
}

//判断一个字符串是否是合法的电话号码
+(BOOL)isTelNumber:(NSString *)telNumberStr
{
    //（1）如果不是11为，return NO;
    if (11 != telNumberStr.length)
    {
        return NO;
    }
    
    //（2）每一位都必须是数字，否则 return NO;
    if (![self isPureInt:telNumberStr])
    {
        return NO;
    }
    
    return YES;
}

//判断一个字符串是否是纯数字的
+(BOOL)isPureInt:(NSString *)telNumberStr
{
    NSScanner* scan = [NSScanner scannerWithString:telNumberStr];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

//根据作业指示中服务类型的Id 获取服务类型的汉字字符串
+(NSString *)getServiceTypeNameById:(NSString *)serviceTypeId
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    for (int i = 0; i<[modelIns.arrOriginalDataForServiceType count]; i++)
    {
        if ([[[modelIns.arrOriginalDataForServiceType objectAtIndex:i] objectForKey:@"serviceTypeId"] isEqualToString:serviceTypeId])
        {
            return [[modelIns.arrOriginalDataForServiceType objectAtIndex:i] objectForKey:@"serviceTypeName"];
        }
    }
    
    return nil;
}

//根据作业指示中修理种类的Id 获取修理种类的汉字字符串
+(NSString *)getRepairTypeNameById:(NSString *)repairTypeId
{
    return nil;
}

//根据name在id与name的列表中获取到ID
+(NSString *)getIdByNameInList:(NSArray *)arrIdAndNameList withName:(NSString *)name byNameKey:(NSString *)nameKey byIdKey:(NSString *)idKey
{
    for (int i = 0; i<[arrIdAndNameList count]; i++)
    {
        NSString *strNameTmp = [[arrIdAndNameList objectAtIndex:i] objectForKey:nameKey];
        NSString *strIdTmp = [[arrIdAndNameList objectAtIndex:i] objectForKey:idKey];
        
        NSRange range = [strNameTmp rangeOfString:name];
        
        if (range.length == [name length])
        {
            return strIdTmp;
        }
    }
    
    return nil;
}

//根据id找到该id在列表中的索引
+(NSInteger)getIndexInList:(NSArray *)arrIdAndNameList withId:(NSString *)idXingCan byNameKey:(NSString *)nameKey byIdKey:(NSString *)idKey
{
    for (int i = 0; i<[arrIdAndNameList count]; i++)
    {
        NSString *strIdTmp = [[arrIdAndNameList objectAtIndex:i] objectForKey:idKey];
        
        if ([idXingCan isEqualToString:strIdTmp])
        {
            return i;
        }
    }
    
    return -1;
}

//根据name找到该name在列表中的索引
+(NSInteger)getIndexInList:(NSArray *)arrIdAndNameList withName:(NSString *)name byNameKey:(NSString *)nameKey
{
    for (int i = 0; i<[arrIdAndNameList count]; i++)
    {
        NSString *strNameTmp = [[arrIdAndNameList objectAtIndex:i] objectForKey:nameKey];
        
        NSRange range = [strNameTmp rangeOfString:name];
        
        if (range.length == [name length])
        {
            return i;
        }
    }
    
    return -1;
}

//根据id在列表中的索引获取name
+(NSString *)getNameByIndexInList:(NSArray *)arrIdAndNameList withIndex:(NSInteger)index byNameKey:(NSString *)nameKey byIdKey:(NSString *)idKey
{
    if (index < [arrIdAndNameList count])
    {
        return [[arrIdAndNameList objectAtIndex:index] objectForKey:nameKey];
    }
    
    return nil;
}

//根据id在列表中的索引获取Id
+(NSString *)getIdByIndexInList:(NSArray *)arrIdAndNameList withIndex:(NSInteger)index byNameKey:(NSString *)nameKey byIdKey:(NSString *)idKey
{
    if (index < [arrIdAndNameList count])
    {
        return [[arrIdAndNameList objectAtIndex:index] objectForKey:idKey];
    }
    
    return nil;
}

//根据id获取name
+(NSString *)getNameByIdInList:(NSArray *)arrIdAndNameList withId:(NSString *)idXingCan byNameKey:(NSString *)nameKey byIdKey:(NSString *)idKey
{
    for (int i = 0; i<[arrIdAndNameList count]; i++)
    {
        NSString *strIdTmp = [[arrIdAndNameList objectAtIndex:i] objectForKey:idKey];
        
        if ([idXingCan isEqualToString:strIdTmp])
        {
            return [[arrIdAndNameList objectAtIndex:i] objectForKey:nameKey];
        }
    }
    
    return nil;
}

//将版本号存入本地
+(void)saveVersion:(NSString *)versionFormPara
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *realPath = [documentPath stringByAppendingPathComponent:@"Config.plist"];
    
    NSMutableDictionary *dictplist = [NSMutableDictionary dictionaryWithDictionary:[NSDictionary dictionaryWithContentsOfFile:realPath]];
    if (nil == versionFormPara)
    {
        versionFormPara = @"0.0";
    }
    NSLog(@"%@",versionFormPara);
    
    //设置属性值
    [dictplist setObject:versionFormPara forKey:@"GHAC_AppVersion"];
    
    //写入文件
    [dictplist writeToFile:realPath atomically:YES];
}

//根据Nsstring的改变情况,确定当前的输入框值
+(NSString *)currStrByStr:(NSString *)strOriFormPara andModifyStr:(NSString *)strModifyFormPara withRange:(NSRange)rangeFormPara
{
    NSMutableString *strTmp = [NSMutableString stringWithString:strOriFormPara];
    [strTmp replaceCharactersInRange:rangeFormPara withString:strModifyFormPara];
    return strTmp;
}

@end
